package com.whh.permission.service.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whh.permission.dao.IUserDao;
import com.whh.permission.po.User;
import com.whh.permission.po.UserRole;
import com.whh.permission.service.IUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements IUserService {
    @Resource
    private IUserDao iUserDao;
    @Override
    public PageInfo<User> findUserAll(int page, int pageSize, User user) {
        PageHelper.startPage(page,pageSize);//分页
        //查询所有用户信息
        List<User> users = iUserDao.queryUserAll(user);
        PageInfo<User> userPageInfo = new PageInfo<>(users);
        return userPageInfo;
    }

    @Override
    @Transactional
    public void addUser(User user) {
        //获取user对项中的角色id集合
        List<Integer> ids = user.getRoleList();
        iUserDao.addUser(user);//添加用户
        //获取用户id
        Integer userId = user.getId();
        List<UserRole> list=new ArrayList<>();
        for (Integer rid:ids){
            list.add(new UserRole(userId,rid));
        }
        //关联角色
        iUserDao.addUserRole(list);
    }

    @Override
    @Transactional
    public void deleteUserInfoByIds(List<Integer> ids) {
        iUserDao.deleteUserByIds(ids);
        iUserDao.deleteUserRoleByUserIds(ids);
    }

    @Override
    public User queryUserByNameAndPwd(String username, String password) {
        return iUserDao.queryUserByNameAndPwd(username,password);
    }

    @Override
    public User queryUserById(Integer id) {
        return iUserDao.queryUserById(id);
    }

    @Override
    public void updateUserSubmit(User user) {
        iUserDao.updateUserSubmit(user);
    }

    @Override
    public List<User> findUrlAndPermByUserId(Integer id) {
        return iUserDao.findUrlAndPermByUserId(id);
    }

    @Override
    public User queryUserByUserName(String name) {
        return iUserDao.queryUserByUserName(name);
    }

}
